package com.friddle.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

public class ServiceUtil {

	public static String getPublicNetwork() throws Exception {
		URL url = new URL("http://www.ip138.com/ip2city.asp");
		URLConnection conn = url.openConnection();
		conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15");
		conn.setRequestProperty("Content-Type", "text/html");
		conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		InputStream is = conn.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(is, "GB2312"));
		String line = null;
		while ((line = reader.readLine()) != null) {
			if (line.contains("您的IP地址是")) {
				int start = line.indexOf('[') + 1;
				int end = line.indexOf(']');
				return line.substring(start, end);
			}
		}
		return "";

	}

	public static String getServerAddress() {
		try {
			InetAddress addr = InetAddress.getLocalHost();
			String address = addr.getHostAddress();
			if ("10.153.196.65".equals(address)) {// 121.41.165.59
				return "59";
			} else if ("10.153.197.15".equals(address)) {// 121.41.160.29
				return "29";
			} else if ("10.153.193.21".equals(address)) {// 121.41.160.43
				return "43";
			} else if ("10.241.51.29".equals(address)) {// 223.4.50.12
				return "12";
			} else if ("10.153.202.68".equals(address)) {// 121.41.169.160
				return "160";
			}
			return address;
		} catch (Exception e) {
			return "未知";
		}
	}

	public static String reflectCitys(String city) {
		Map<String, String> city2type = new HashMap<String, String>();
		if (city2type.size() == 0) {
			city2type.put("南京", "nj");
			city2type.put("无锡", "wx");
			city2type.put("杭州", "hz");
			city2type.put("合肥", "hf");
			city2type.put("苏州", "sz");
			city2type.put("常州", "cz");
			city2type.put("昆山", "ks");
			city2type.put("芜湖", "wh");
			city2type.put("重庆", "cq");
			city2type.put("西安", "xa");
			city2type.put("沈阳", "sy");
			city2type.put("蚌埠", "bb");
		}
		return city2type.get(city);
	}

}
